package org.asiainfo.ability.manager.mq.impl;

import org.asiainfo.ability.base.queue.OperEvent;
import org.asiainfo.ability.gateway.configuration.OperConfig;
import org.asiainfo.ability.gateway.utils.SerializationUtils;
import org.asiainfo.ability.manager.mq.MessageQueueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * @Author: visen
 * @Date: 2018/3/11
 * @Description:
 */
@Service
public class RedisMqService implements MessageQueueService {
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void publishEvent(OperEvent event) {
        redisTemplate.execute(new RedisCallback<Object>() {
            public Object doInRedis(RedisConnection connection) {
                connection.publish(OperConfig.topicName.getBytes(), SerializationUtils.serializeNative(event));
                return null;
            }
        }, true);
    }
}
